import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/custom_widget.dart';
import 'package:osg/controllers/http.dart';
import 'package:osg/controllers/my_method.dart';

class Resale extends StatefulWidget {
  Resale({super.key});

  @override
  State<Resale> createState() => _WebDetailState();
}

class _WebDetailState extends State<Resale> {

  final piliang = Get.parameters['piliang']??0;
  

  var thumb = '';
  var id = '';
  var title = '';
  var nftId = '';
  var minratio = '';
  var maxratio = '';

  String brokerageratio ='';
   String editiontaxratio = '';
  var sellnum = '';

  RxString money = ''.obs;
  RxString pay = ''.obs;

  late TextEditingController _controller;
  late TextEditingController controller;
  
  String? uuid;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    controller = TextEditingController();

      
       WidgetsBinding.instance.addPostFrameCallback((_) {
         if(piliang=='1'){
           uuid = Get.parameters['uuid'];
          Get.find<HTTPS>().getData('/packages/info/$uuid', {}).then((res){
            if(res!=null){  
              title = res['title'];
                nftId = res['nftId'];
                minratio = res['examine_min_ratio'];
                maxratio = res['examine_max_ratio'];
                sellnum = res['sell_num'];
                editiontaxratio = res['edition_tax_ratio'];
                brokerageratio = res['brokerage_ratio'];
                thumb = res['thumb'];
            }
      });

     }else{
      id = Get.parameters['id']!;
      title = Get.parameters['title']!;
      nftId = Get.parameters['nftId']!;
      minratio = Get.parameters['examine_min_ratio']!;
      maxratio = Get.parameters['examine_max_ratio']!;
      sellnum = Get.parameters['sell_num']!;
      editiontaxratio = Get.parameters['edition_tax_ratio']!;
      brokerageratio = Get.parameters['brokerage_ratio']!;
      thumb = Get.parameters['img']!;
     }
    });

    
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
    controller.dispose();
  }

  @override
  Widget build(BuildContext context) {

 



    return Scaffold(
        appBar: AppBar(
            backgroundColor: Color(0xffffffff),
            title: Text('寄售'),
            centerTitle: true),
        body: Padding(
          padding: EdgeInsets.symmetric(horizontal: 15),
          child: Column(
            children: [
              Expanded(
                  child: ListView(
                children: [
                  Divider(
                    color: Color(0XFFDDDDDD),
                    height: 30,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.circular(5.0), // 设置圆角大小
                        child: Image.network(thumb!,
                            width: 80, height: 80, fit: BoxFit.fill,
                            errorBuilder: (context, error, stackTrace) {
                          return SizedBox(
                            width: 80,
                          );
                        }),
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '$title',
                            style: TextStyle(
                                fontSize: 15, color: Color(0XFF0F172B)),
                          ),
                          SizedBox(
                            height: 20,
                          ),
                          Row(
                            children: [
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '限量',
                                    style: TextStyle(
                                        fontSize: 11, color: Color(0XFF999999)),
                                  ),
                                  Text(
                                    '$sellnum',
                                    style: TextStyle(
                                        fontSize: 14, color: Color(0XFF0F172B)),
                                  ),
                                ],
                              ),
                              SizedBox(
                                width: 40,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '藏品编号',
                                    style: TextStyle(
                                        fontSize: 11, color: Color(0XFF999999)),
                                  ),
                                  Text(
                                    '#${Get.find<MyMethod>().hao(nftId!)}',
                                    style: TextStyle(
                                        fontSize: 14, color: Color(0XFF0F172B)),
                                  ),
                                ],
                              ),
                              SizedBox(
                                width: 40,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '获取方式',
                                    style: TextStyle(
                                        fontSize: 11, color: Color(0XFF999999)),
                                  ),
                                  Text(
                                    '购买',
                                    style: TextStyle(
                                        fontSize: 14, color: Color(0XFF0F172B)),
                                  ),
                                ],
                              ),
                            ],
                          )
                        ],
                      )
                    ],
                  ),
                  Divider(
                    color: Color(0XFFDDDDDD),
                    height: 30,
                  ),
                  Row(
                    children: [
                      Text(
                        '寄售价格',
                        style:
                            TextStyle(fontSize: 14, color: Color(0XFF999999)),
                      ),
                      Expanded(
                          child: TextField(
                        onChanged: (vals) {
                          num numericValue = num.parse(brokerageratio);
                          num taxratio = num.parse(editiontaxratio);

                          num val = num.parse(vals);
                          num v = 100 - numericValue + taxratio;

                          num b = ((val * v) / 100);

                          num numcc = val - b;
                          pay.value = numcc.toStringAsFixed(2);

                          money.value = ((val * v) / 100).toStringAsFixed(2);

                          // (val * (100 - numericValue) / 100)
                        },
                        controller: _controller,
                        textAlign: TextAlign.end,
                        style: TextStyle(fontSize: 14),
                        decoration: InputDecoration(
                          isCollapsed: true, //设置高度
                          hintStyle: TextStyle(color: Color(0XFF0F172B)),
                          border: InputBorder.none, // 隐藏输入框默认边框
                          hintText: '0.00', // 提示文本
                        ),
                      )),
                      Text(
                        '元',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF999999)),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 3,
                  ),
                  Text('寄售价必须大于$minratio元或小于$maxratio元',
                      style: TextStyle(fontSize: 11, color: Color(0XFF999999)),
                      textAlign: TextAlign.end),
                  SizedBox(
                    height: 15,
                  ),
                  Row(
                    children: [
                      Text(
                        '版权费与平台服务费',
                        style:
                            TextStyle(fontSize: 14, color: Color(0XFF999999)),
                      ),
                      Expanded(
                        child: Obx(() => Text('-$pay',
                            style: TextStyle(
                                fontSize: 12,
                                color: Color(0XFF0F172B),
                                fontWeight: FontWeight.bold),
                            textAlign: TextAlign.end)),
                      ),
                      Text(
                        '元',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF999999)),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Row(
                    children: [
                      Text(
                        '实际到账',
                        style:
                            TextStyle(fontSize: 14, color: Color(0XFF999999)),
                      ),
                      Expanded(
                        child: Obx(() => Text('$money',
                            style: TextStyle(
                                fontSize: 12,
                                color: Color(0XFF0F172B),
                                fontWeight: FontWeight.bold),
                            textAlign: TextAlign.end)),
                      ),
                      Text(
                        '元',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF999999)),
                      ),
                    ],
                  ),
                  Divider(
                    color: Color(0XFFDDDDDD),
                    height: 50,
                  ),
                  Text(
                    '说明',
                    style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Text(
                    '1、转出时请注意核对信息，藏品一旦转出将无法找回',
                    style: TextStyle(fontSize: 12, color: Color(0XFF999999)),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Text(
                    '2、交易成功后，将扣除1%的版权费以及1%的平台服务费',
                    style: TextStyle(fontSize: 12, color: Color(0XFF999999)),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                ],
              )),
              Row(
                children: [
                  Expanded(
                      child: Container(
                    alignment: Alignment.center,
                    margin: EdgeInsets.symmetric(vertical: 10),
                    height: 60,
                    decoration: BoxDecoration(
                        color: Color(0XFF0F172B),
                        borderRadius: BorderRadius.circular(60)),
                    child: GestureDetector(
                      onTap: () => {
                        if (_controller.text.isNotEmpty)
                          {
                            Get.dialog(
                              AlertDialog(
                                  backgroundColor: Colors.transparent,
                                  shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(5)),
                                  content: Stack(
                                    children: [
                                      Image.asset(
                                        'static/tan.png',
                                        width: 255,
                                        fit: BoxFit.fitWidth,
                                      ),
                                      Positioned(
                                          top: 50,
                                          right: 0,
                                          left: 0,
                                          child: Column(
                                            children: [
                                              Text(
                                                '请输入交易密码',
                                                style: TextStyle(
                                                    fontSize: 24,
                                                    color: Color(0XFF0F172B)),
                                              ),
                                              SizedBox(
                                                height: 30,
                                              ),
                                              Container(
                                                margin: EdgeInsets.symmetric(
                                                    horizontal: 30),
                                                decoration: BoxDecoration(
                                                    color: Color(0XFFF8F9FD),
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            10)),
                                                child: TextField(
                                                  controller: controller,
                                                  obscureText: true,
                                                  style:
                                                      TextStyle(fontSize: 15),
                                                  decoration: InputDecoration(
                                                    isCollapsed: true, //设置高度
                                                    contentPadding:
                                                        EdgeInsets.all(15),
                                                    hintStyle: TextStyle(
                                                        color:
                                                            Color(0xff999999)),
                                                    border: InputBorder
                                                        .none, // 隐藏输入框默认边框
                                                    hintText: '输入交易密码', // 提示文本
                                                  ),
                                                ),
                                              ),
                                              SizedBox(
                                                height: 30,
                                              ),
                                              GestureDetector(
                                                onTap: () {
                                                  if (controller
                                                      .text.isNotEmpty) {
                                                    Get.find<HTTPS>().postData(
                                                        '/packages/resales', {
                                                      "package_sku_id": id,
                                                      "level2password":
                                                          controller.text,
                                                      "metaAddress": "",
                                                      "money_price":
                                                          _controller.text,
                                                    }).then((res) {
                                                      if (res != null) {
                                                        Get.offAllNamed('/');
                                                        Get.find<HTTPS>()
                                                            .showTost('发布成功');
                                                      }
                                                    });
                                                  } else {
                                                    Get.find<HTTPS>()
                                                        .showTost('请输交易密码');
                                                  }
                                                },
                                                child: Container(
                                                  width: 210,
                                                  height: 45,
                                                  decoration: BoxDecoration(
                                                      color: Color(0XFF0F172B),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              45)),
                                                  alignment: Alignment.center,
                                                  child: Text('确认',
                                                      style: TextStyle(
                                                          fontSize: 18,
                                                          color: Color(
                                                              0XFFFFFFFF))),
                                                ),
                                              ),
                                              SizedBox(
                                                height: 10,
                                              ),
                                              GestureDetector(
                                                onTap: () {
                                                  Get.back();
                                                },
                                                child: Text('取消',
                                                    style: TextStyle(
                                                        fontSize: 18,
                                                        color:
                                                            Color(0XFF0F172B))),
                                              ),
                                            ],
                                          )),
                                    ],
                                  )),
                            )
                          }
                        else
                          {Get.find<HTTPS>().showTost('请输价格')}
                      },
                      child: Text(
                        '发布寄售信息',
                        style:
                            TextStyle(fontSize: 18, color: Color(0XFFFFFFFF)),
                      ),
                    ),
                  ))
                ],
              )
            ],
          ),
        ));
  }
}
